package com.itlong.cloud.utils.random;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Random;

/**
 * <desc>
 *      随机字生成帮助类。
 * </desc>
 *
 * @createDate 2017/10/11
 */
public class RandomUtil {

    /**
     * <desc>
     *      获取范围内的随机数。
     * <desc/>
     *
     * @param max 最大值
     * @param min 最小值
     * @return
     * @author Wenchao.L
     * @createDate 2017/10/11
     */
    public static Integer getRandomAround(Integer max, Integer min) {
        Random random = new Random();
        return random.nextInt(max) % (max - min + 1) + min;
    }

    /**
     * <desc>
     *      获取指定长度的随机字符串。
     * <desc/>
     *
     * @param length   长度
     * @return
     * @author Wenchao.L
     * @createDate 2017/10/11
     */
    public static String getRandomStringByLength(int length) {
        String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 微信支付 将金额转换成分
     * @param fee 元格式的
     * @return 分
     * @author Juguang.S
     * @createDate 2018-04-18
     */
    public static String changeToFen(Double fee){
        String priceStr = "";
        if(fee != null){
            int p = (int)(fee * 100); //价格变为分
            priceStr = Integer.toString(p);
        }
        return priceStr;
    }

    /**
     * 微信支付 得到本地机器的IP
     * @return
     * @author Juguang.S
     * @createDate 2018-04-18
     */
    public static String getHostIp(){
        String ip = "";
        try{
            ip = InetAddress.getLocalHost().getHostAddress();
        }catch(UnknownHostException e){
            e.printStackTrace();
        }
        return ip;
    }

    /**
     * <desc>
     *      获取6位随机数字
     * <desc/>
     *
     * @param length   长度
     * @return
     * @author Juguang.S
     * @createDate 2019/04/29
     */
    public static String getRandomStringBySix(int length) {
        String base = "0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }


}
